import 'dart:math';

import 'package:flutter/material.dart';
import 'package:whale/helper/utils/util.dart';

class NoticeView extends StatefulWidget {
  const NoticeView({
    super.key,
    required this.child,
  });
  final Widget child;
  @override
  State<NoticeView> createState() => _NoticeViewState();
}

class _NoticeViewState extends State<NoticeView>
    with SingleTickerProviderStateMixin {
  double offsetY = 0;
  double startY = 0;
  double padding = 20.r;

  double get top {
    var mq = MediaQuery.of(context);
    return mq.viewPadding.top;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onVerticalDragStart: (details) {
        startY = details.localPosition.dy;
      },
      onVerticalDragUpdate: (details) {
        setState(() {
          offsetY = details.localPosition.dy - startY;
        });
      },
      onVerticalDragEnd: (details) {
        if (offsetY < -top / 2) {
          Get.back();
        } else {
          setState(() {
            offsetY = 0;
          });
        }
      },
      onDoubleTapCancel: () {
        setState(() {
          offsetY = 0;
        });
      },
      child: Stack(
        children: [
          Positioned(
            top: min(top + offsetY, top),
            left: padding,
            right: padding,
            child: widget.child,
          ),
        ],
      ),
    );
  }
}
